<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">

    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <title>自动分类规则编辑</title>
    <link href="../style/layui/css/layui.css" rel="stylesheet">
    <link href="../style/css/style.css" rel="stylesheet">
</head>
<body>
<div style="padding: 15px;">
    <form action="" class="layui-form layui-form-pane" id="form" lay-filter="form">
        <input autocomplete="off" class="layui-input" id="id" name="id" style="display: none"
               type="text" value="">
        <input autocomplete="off" class="layui-input" id="dataId" name="dataId"
               style="display: none" type="text" value="">
        <input autocomplete="off" class="layui-input" id="version" name="version"
               style="display: none" type="text" value="1">

        <div class="label-title">规则名称</div>
        <input autocomplete="off" class="layui-input" id="regular_name" name="regular_name"
               placeholder="分类规则名称" type="text">
        <div class="label-title">规则说明</div>
        <textarea class="layui-textarea" id="regular_remark" name="regular_remark"
                  placeholder="使用注意事项、作者信息等..."></textarea>

        <div class="label-title">时间范围</div>

        <div class="outline" style="text-align: center">
            <input autocomplete="off" class="layui-input time" id="regular_time1"
                   name="regular_time1" placeholder="最小时间" readonly="readonly" type="text"
                   value="00:00">
            <span style="margin: 10px;padding-left: 5px;">-</span>
            <input autocomplete="off" class="layui-input time" id="regular_time2"
                   name="regular_time2" placeholder="最大时间" readonly="readonly" type="text"
                   value="23:59">
        </div>

        <div class="label-title">金额满足条件</div>

        <div class="float-parent" style="position: relative;">
            <select class="float-div" id="regular_money1_link" lay-filter="condition_money_max"
                    name="regular_money1_link">
                <option selected="" value="=">等于</option>
                <option value=">=">大于等于</option>
                <option value="<=">小于等于</option>
            </select>
            <input autocomplete="off" class="layui-input" id="regular_money1" name="regular_money1"
                   placeholder="不需要请留空"
                   style="padding-left: 95px" type="number">

        </div>
        <div class="float-parent" style="position: relative;margin-top: 5px">
            <select class="float-div" id="regular_money2_link" lay-filter="condition_money_min"
                    name="regular_money2_link">
                <option selected="" value="=">等于</option>
                <option value=">=">大于等于</option>
                <option value="<=">小于等于</option>
            </select>
            <input autocomplete="off" class="layui-input" id="regular_money2" name="regular_money2"
                   placeholder="不需要请留空"
                   style="padding-left: 95px" type="number">

        </div>

        <div class="label-title">商户名称满足条件</div>
        <div class="float-parent" style="position: relative;">
            <select class="float-div" id="regular_shopName_link" lay-filter="condition_shopName"
                    name="regular_shopName_link">
                <option selected="" value="包含">包含</option>
                <option value="正则匹配">正则匹配</option>
            </select>
            <input autocomplete="off" class="layui-input" id="regular_shopName"
                   name="regular_shopName" placeholder="不需要请留空"
                   style="padding-left: 95px" type="text">

        </div>

        <div class="label-title">商户备注满足条件</div>

        <div class="float-parent" style="position: relative;margin-bottom: 10px">
            <select class="float-div" id="regular_shopRemark_link" lay-filter="condition_shopRemark"
                    name="regular_shopRemark_link">
                <option selected="" value="包含">包含</option>
                <option value="正则匹配">正则匹配</option>
            </select>
            <input autocomplete="off" class="layui-input" name="regular_shopRemark"
                   placeholder="不需要请留空"
                   style="padding-left: 95px" type="text">

        </div>

        <div class="layui-form-item">
            <label class="layui-form-label title">消费类型为</label>
            <div class="layui-input-block line">
                <select id="regular_type" lay-filter="type" name="regular_type">
                    <option selected="" value="支出">支出</option>
                    <option value="收入">收入</option>
                    <option value="报销">报销</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label title">分类为</label>
            <div class="layui-input-block line">
                <div style="position: relative;">
                    <img alt="" class="img"
                         src="https://pic.dreamn.cn/uPic/2021032310470716164676271616467627123WiARFwd8b1f5bdd0fca9378a915d8531cb740b.png"
                    />
                    <input autocomplete="off" class="layui-input" id="regular_sort"
                           name="regular_sort" placeholder="分类规则名称"
                           readonly="readonly" style="padding-left: 30px" type="text">

                </div>
            </div>
        </div>


        <div class="layui-form-item" style="text-align: center">
            <button class="layui-btn" id="test" lay-filter="test" type="button">测试</button>
            <button class="layui-btn" id="save" lay-filter="save" type="button">保存</button>
        </div>
    </form>
</div>
<script src="../style/layui/layui.js"></script>
<script>

    let webviewCallback;//用于外部调用
    let url = window.location.href, baseurl = '../js/';
    if(url.indexOf("/Auto-CateJs/")!==-1||url.indexOf("android_asset")!==-1){
        baseurl='../style/js/';
    }
    //解决相对路径问题
    //调用
    layui.config({
        base: baseurl //假设这是test.js所在的目录   可以把你需要扩展的js插件都放在一个文件夹内
    }).extend({ //设定组件别名
        common: 'common',
        baseFamily: 'baseFamily',
        webview: 'webview'
    });
    layui.use(['layer', 'form', 'common','baseFamily','webview'], function () {
        const layer = layui.layer
            , form = layui.form
            , common = layui.common
            , webview = layui.webview
        ,baseFamily = layui.baseFamily;
        layui.$('.time').on("click", function (e) {

            webview.selectTime(this.id);

        });
        webviewCallback = {
            setCategory:function (name) {
                layui.$("input[name=regular_sort]").val(name);
            },//设置选择分类
            setData:function (data) {
                common.restoreFromData("form",baseFamily.decrypt(data,"base16"));
            },//设置数据
            setTime:function (dom,time){
                layui.$("#"+dom).val(time);
            }
        };
        webview.initData();
        String.format = function () {
            let s = arguments[0];
            for (let i = 0; i < arguments.length - 1; i++) {
                const reg = new RegExp("%s", "m");
                s = s.replace(reg, arguments[i + 1]);
            }
            return s;
        };
        //设置自动保存
        common.restore("form", "auto_cate");
        common.autoSave("form", "auto_cate");
        //自动保存
        //按键监听
        layui.$("#regular_sort").on("click", function (e) {
            try {
                const data = form.val("form");
                webview.selectCategory(data.regular_type);
            } catch (e) {
                console.log(e.toString());
            }

        });
        function getJs() {
            const data = form.val("form");
            //获取名称
            const name = data.regular_name;
            if (name === "") {
                webview.toast("名称不能为空");
                return "";
            }
            //获取分类
            const sort = data.regular_sort;
            if (sort === "") {
                webview.toast("分类不能为空");
                return "";
            }


            let regularList = "";

            //时间获取
            if (data.regular_time1 !== "" && data.regular_time2 !== "")
                regularList += String.format("isInTimeInner('%s','%s',hour,minute)&& ", data.regular_time1, data.regular_time2);


            //时间获取
            if (data.regular_money1!== "")
                regularList += String.format("money %s %s && ", data.regular_money1_link, data.regular_money1);
            if (data.regular_money2 !== "")
                regularList += String.format("money %s %s && ", data.regular_money2_link, data.regular_money2);

            const shopName = data.regular_shopName;
            const shopNameLink = data.regular_shopName_link;
            if (shopName !== "") {
                if (shopNameLink === "包含") {
                    regularList += String.format("shopName.indexOf('%s')!=-1 && ", shopName);

                } else if (shopNameLink === "正则匹配") {
                    regularList += String.format("(/%s/g).test(shopName) && ", shopName);
                }
            }
            const shopRemark = data.regular_shopRemark;
            const shopRemarkLink = data.regular_shopRemark_link;
            if (shopRemark !== "") {
                if (shopRemarkLink === "包含") {
                    regularList += String.format("shopRemark.indexOf('%s')!=-1 && ", shopRemark);

                } else if (shopRemarkLink === "正则匹配") {
                    regularList += String.format("(/%s/g).test(shopRemark) && ", shopRemark);
                }
            }

            //获取类型
            const type = data.regular_type;
            regularList += String.format("type == '%s' && ", type);

            const last = regularList.lastIndexOf('&');
            if (last !== -1 && last !== 0)
                regularList = regularList.substring(0, last - 1);

            if (regularList === "") {
                webview.toast("规则填写错误:条件内容为空。");
                return "";
            }

            return String.format("if(%s)return '%s';", regularList, sort);
        }
        layui.$("#save").on("click", function (e) {

            var data=JSON.stringify(form.val("form"));
            webview.saveCategory(getJs(),data);
            common.cleanSave("auto_cate");

        });
        layui.$("#test").on("click", function (e) {

            try {
                const js = getJs();
                if(js!=="") webview.testCategory(js);
            } catch (e) {
                console.log(e.toString());
            }

        });


    });
</script>
</body>
</html>